<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of dating
 *
 * @author 10520_000
 */
class dating extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('connect');
        $this->load->helper("url");
        $this->load->helper("date");
        $this->load->database();
    }

    function index() {
        $this->load->view('dating');
    }

    function createdating() {

        $date = date("Y-m-d", strtotime($_POST['date']));
        $data = array(
            'dating_name' => $_POST['namedating'],
            'date_dating' => $date,
            'hours' => $_POST['hours'],
            'minute' => $_POST['minute'],
            'description' => $_POST['description'],
            'target' => $_POST['gender'],
            'address' => $_POST['address'],
            'user_id1' => $this->session->userdata('id'),
        );

        $this->db->insert('dating', $data);
    }

    function loadmore() {
        $display = 4;
        $a = $this->connect->loaddating($display, $_POST['id']);
        foreach ($a as $i) {
            $id_dating = $i->dating_id;
            $id_create = $i->user_id1;
            $datingname = $i->dating_name;
            $description = $i->description;
            $target = $i->target;
            $date = $i->date_dating;
            $hours = $i->hours;
            $minute = $i->minute;
            $address = $i->address;

            $time = date("d-m-Y", strtotime($date));
            $b = $this->connect->getID($id_create);
            foreach ($b as $z) {
                $avarta = $z->avarta;
                $name = $z->name;
                $old = $z->age;
                $thanhpho = $z->city;
            }
            echo "<li class = 'dating' >";
            echo "<div style = 'float: left; width: 10%;'>";
            if ($id_create == $this->session->userdata('id')) {
                echo "<a href='" . base_url('home/mypage') . "'>";
            } else {
                echo "<a href='" . base_url('getid?id=' . $id_create) . "'>";
            }

            echo "<img class = 'anhnho4 img-thumbnail' src ='http://localhost:8080/nfa/images/avarta/" . $avarta . "' />";
            echo "</a>";
            echo "</div>";
            echo "<div style = 'float: left; width: 40%; padding-left: 2%;'>";
            echo "<a class ='open'> " . $datingname . "</a> <br/>";
            echo "<b> Hẹn bởi :</b>" . $name . "," . $old . " tuổi <br/>";

            echo "<b> Hẹn với : </b>" . $target . "<br/>";
            echo "<b>Mô tả : </b> <p class='short-text'>" . $description . "</p>";


            echo "</div>";
            echo "<div style='float: left; width: 35%;'>";
            echo "<b> Thời gian :</b> <?php echo $hours; ?> giờ <?php echo $minute; ?> phút.<br/>";
            echo "<b>Ngày :</b>" . $time . "<br/>";
            echo "<b> Địa điểm hẹn :" . $address . " -" . $thanhpho . "</b>";

            echo "</div>";
            echo "<div style='float: left; width: 10%;'>";
            echo "<a class='btn btn-warning' href='" . base_url('dating/getdating?id=' . $id_dating) . "' >Xem chi tiết</a>";
            echo "</div>";

            echo "</li>";
        }
    }

    function searchdating() {

        $from = date("Y-m-d", strtotime($_POST['from']));
        $to = date("Y-m-d", strtotime($_POST['to']));
        $this->session->unset_userdata('searchdating');
        if ($_POST['gender'] != NULL && $_POST['city'] == NULL) {
            $s = $this->connect->searchdating1($_POST['gender']);
            $this->session->set_userdata('searchdating', $s);
            $this->load->view('dating-result');
        } elseif ($_POST['gender'] != NULL && $_POST['city'] != NULL) {
            $s = $this->connect->searchdating2($_POST['gender'], $_POST['city']);
            $this->session->set_userdata('searchdating', $s);
            $this->load->view('dating-result');
        } elseif ($_POST['gender'] != NULL && $_POST['city'] != NULL && $from != NULL && $to == NULL) {
            $s = $this->connect->searchdating3($_POST['gender'], $_POST['city'], $from);
            $this->session->set_userdata('searchdating', $s);
            $this->load->view('dating-result');
        } elseif ($_POST['gender'] != NULL && $_POST['city'] != NULL && $from == NULL && $to != NULL) {
            $s = $this->connect->searchdating4($_POST['gender'], $_POST['city'], $to);
            $this->session->set_userdata('searchdating', $s);
            $this->load->view('dating-result');
        } elseif ($_POST['gender'] != NULL && $_POST['city'] != NULL && $from != NULL && $to != NULL) {
            $s = $this->connect->searchdating($_POST['gender'], $_POST['city'], $from, $to);
            $this->session->set_userdata('searchdating', $s);
            $this->load->view('dating-result');
        }
    }

    function getdating() {
        $data['a'] = $this->connect->getdating2($_GET['id']);


        $this->load->view("dating-details", $data);
    }

    function loadcomment() {
        $a = $this->connect->loadcomment($_POST['id']);
        foreach ($a as $i) {
            $id = $i->user_id;
            $content = $i->content;
            $date = $i->date;
            $b = $this->connect->getID($id);
            foreach ($b as $z) {
                $avarta = $z->avarta;
                $name = $z->name;
                $old = $z->age;
                $thanhpho = $z->city;
            }
            echo "<li class = 'cmt' >";

            echo "<div style = 'float: left; width: 10%;'>";
            if ($id == $this->session->userdata('id')) {
                echo "<a href='" . base_url('home/mypage') . "'>";
            } else {
                echo "<a href='" . base_url('getid?id=' . $id) . "'>";
            }

            echo "<img class = 'anhnho4 img-thumbnail' src ='http://localhost:8080/nfa/images/avarta/" . $avarta . "' />";
            echo "</a>";
            echo "</div>";
            echo "<div style = 'float: left; width: 80%; padding-left: 2%;'>";
            echo "<a class ='open'> " . $name . "-" . $old . " tuổi - " . $thanhpho . "</a> <br/>";
            echo $content;
            echo "<p style='color: #149bdf;'>Đã gửi : " . $date . "</p><br/>";
            echo "<p style='  border-bottom: 1px #DDDDDD solid;'></p>";

            echo "</div>";



            echo "</li>";
        }
    }

    function savecomment() {
        $now = date("Y-m-d H:i:s");
        $data1 = array(
            'user_id' => $this->session->userdata('id'),
            'dating_id' => $_POST['dating-id'],
            'content' => $_POST['content'],
            'date' => $now
        );
        $this->db->insert('comment', $data1);

        $data2 = array(
            'id_send' => $this->session->userdata('id'),
            'id_receive' => $_POST['create-id'],
            'type_noti' => 3,
            'date' => $now,
            'status' => 'Chưa xem',
            'notice_id' => $_POST['dating-id']
        );
        $this->db->insert('notification', $data2);
    }

    function loadnotification() {
        $a = $this->connect->getnotification($this->session->userdata('id'));
        foreach ($a as $i) {
            $notificationID = $i->id;
            $id = $i->id_send;
            $type = $i->type_noti;
            $notice_id = $i->notice_id;
            $date = $i->date;

            $b = $this->connect->getID($id);
            foreach ($b as $y) {
                $ten = $y->name;
                $anh = $y->avarta;
            }


            if ($type == 1) {
                echo "<a  style='text-decoration: none;' onclick='clicknotification(" . $notificationID . ");' href='" . base_url('getID?id=' . $id) . "'>";
                echo "<li class='notice-list'>";
                echo "<div class='anh-notice'>";
                echo "<img class= 'anhnho3 img-thumbnail' src='http://localhost:8080/nfa/images/avarta/" . $anh . "' >";
                echo "</div>";
                echo "<div class ='ten-noice'>";
                echo "<p style='color: #003399;'>" . $ten . " vừa đồng ý lời mời kết bạn của bạn. Hãy hẹn hò ngay! </p>";
                echo "<p '> Vào lúc : " . $date . " </p>";
                echo "</div>";
                echo "</liv>";
                echo "</a>";
            } elseif ($type == 2) {
                echo "<a  style='text-decoration: none;' onclick='clicknotification(" . $notificationID . ");'  href='" . base_url('dating/getdating?id=' . $notice_id) . "'>";
                echo "<li class='notice-list'>";
                echo "<div class='anh-notice'>";
                echo "<img class= 'anhnho3 img-thumbnail' src='http://localhost:8080/nfa/images/avarta/" . $anh . "' >";
                echo "</div>";
                echo "<div class ='ten-noice'>";
                echo "<p style='color: #003399;'>" . $ten . " muốn hẹn hò với bạn trong một cuộc hen. </p>";
                echo "<p '> Vào lúc : " . $date . " </p>";
                echo "</div>";
                echo "</liv>";
                echo "</a>";
            } else {
                if ($id != $this->session->userdata('id')) {
                    echo "<a  style='text-decoration: none;' onclick='clicknotification(" . $notificationID . ");'  href='" . base_url('dating/getdating?id=' . $notice_id) . "'>";
                    echo "<li class='notice-list'>";
                    echo "<div class='anh-notice'>";

                    echo "<img class= 'anhnho3 img-thumbnail' src='http://localhost:8080/nfa/images/avarta/" . $anh . "' >";

                    echo "</div>";
                    echo "<div class ='ten-noice'>";

                    echo "<p ><b>" . $ten . "</b> đã bình luận cuộc hẹn hò của bạn. </p>";
                    echo "<p style='color: #003399;'> Vào lúc : " . $date . " </p>";
                    echo "</div>";

                    echo "</liv>";
                    echo "</a>";
                }
            }
        }
    }

    function checknotice() {
        $a = $this->connect->checknotification($this->session->userdata('id'));
        if (count($a) > 0) {
            echo count($a);
        } else {
            echo '';
        }
    }

    function joindating() {
        $now = date("Y-m-d H:i:s");

        $data1 = array(
            'user_id' => $this->session->userdata('id'),
            'dating_id' => $_POST['id_dating'],
            'date' => $now
        );
        $this->db->insert('joindating', $data1);

        $data2 = array(
            'id_send' => $this->session->userdata('id'),
            'id_receive' => $_POST['id'],
            'type_noti' => 2,
            'date' => $now,
            'status' => 'Chưa xem',
            'notice_id' => $_POST['id_dating']
        );
        $this->db->insert('notification', $data2);
    }

    function getbutton() {

        $b = $this->connect->getlistjoin($this->session->userdata('id'), $_POST['id_dating']);






        if ($_POST['id'] == $this->session->userdata('id')) {

            echo "";
        } else {
            if ($b) {
                echo "<button class='btn btn-primary'><span class='glyphicon glyphicon-ok'></span> Đã tham gia</button>";
            } else {
                echo "<button class='btn btn-primary' data-toggle='modal' data-target='#joindating'><span class='glyphicon glyphicon-arrow-down'></span> Tham gia ngay</button>";
            }
        }
    }

    function getlistjoin() {
        $d = $this->connect->getlistjoin1($_POST['id_dating']);
        if ($d) {
            foreach ($d as $z) {
                $id_send = $z->user_id;

                $a = $this->connect->getID($id_send);
                foreach ($a as $y) {
                    $ten = $y->name;
                    $anh = $y->avarta;
                }
                echo "<li class ='listjoin'>";
                echo "<a  alt='" . $ten . "' style='text-decoration: none;' href='" . base_url('getID?id=' . $id_send) . "'>";
                echo "<img class= 'anhnho2 img-thumbnail'  src='http://localhost:8080/nfa/images/avarta/" . $anh . "' >";
                echo "</a>";
                echo "</li>";
            }
        }
    }

    function clicknotification() {
        $data2 = array(
            'status' => 'Đã xem'
        );
        $this->db->where('id', $_POST['id']);
        $this->db->update('notification', $data2);
    }

  

}

?>
